package ServerData;

import Server.Domain.User;

import java.util.concurrent.ConcurrentHashMap;

public class ServerOnlineData {
    ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
    public ServerOnlineData(){
        //添加测试集
        map.put("小明",0);
        map.put("小红",0);
        map.put("小李",0);
    }
    public void online(User user){
        if(map.containsKey(user.getName())){
            map.replace(user.getName(),1);
        }
    }
    public void offline(User user){
        if(map.containsKey(user.getName())){
            map.replace(user.getName(),0);
        }
    }
    public String getOnline(){
        StringBuilder sb = new StringBuilder();
        for(String key:map.keySet()){
            if(map.get(key)==1){
                sb.append(key).append("  ");
            }
        }
        return sb.toString();
    }
}
